﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSTrinhDoTinHoc : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string TenTrinhDoTinHoc { set; get; }
        public void Edit(int hdId, int trinhDoID, string ghiChu, DateTime ngayHieuLuc)
        {
            List<NSTrinhDoTinHoc> lst = (List<NSTrinhDoTinHoc>)HttpContext.Current.Session["TDTH"];
            NSTrinhDoTinHoc pc = lst.Where(a => a.TrinhDoTinHocID.Equals(hdId)).SingleOrDefault();
            pc.LoaiTrinhDoTinHocID = trinhDoID;
            pc.GhiChu = ghiChu;
            pc.TenTrinhDoTinHoc = common.LoadCommonCodeById(trinhDoID);
            if (!ngayHieuLuc.Year.Equals(1))
            {
                pc.NgayHieuLuc = ngayHieuLuc;
            }

        }
        public void Delete(int hdId)
        {
            List<NSTrinhDoTinHoc> lst = (List<NSTrinhDoTinHoc>)HttpContext.Current.Session["TDTH"];
            NSTrinhDoTinHoc pc = lst.Where(a => a.TrinhDoTinHocID.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAll(int nhanVienId)
        {
            var query = (from a in db.NSTrinhDoTinHocs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSTrinhDoTinHocs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void Update(int nhanVienId, List<NSTrinhDoTinHoc> lstObject)
        {

            var query = (from a in db.NSTrinhDoTinHocs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSTrinhDoTinHocs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

            foreach (NSTrinhDoTinHoc pc in lstObject)
            {
                NSTrinhDoTinHoc item = new NSTrinhDoTinHoc();
                Copy(pc, item);
                db.NSTrinhDoTinHocs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        public List<NSTrinhDoTinHoc> LoadData(int nhanVienId)
        {

            var query = (from a in db.NSTrinhDoTinHocs
                         where a.NhanVienID.Equals(nhanVienId)

                         select a).ToList();
            foreach (NSTrinhDoTinHoc item in query)
            {
                item.TenTrinhDoTinHoc = common.LoadCommonCodeById((int)item.LoaiTrinhDoTinHocID);
            }

            return query;

        }
        public NSTrinhDoTinHoc GetItem(int nhanVienId, int trinhDoID, string ghiChu, DateTime ngayHieuLuc)
        {

            NSTrinhDoTinHoc hd = new NSTrinhDoTinHoc();
            hd.TrinhDoTinHocID = 0 - DateTime.Now.Second;
            hd.GhiChu = ghiChu;
            hd.NhanVienID = nhanVienId;
            hd.LoaiTrinhDoTinHocID = trinhDoID;
            hd.TenTrinhDoTinHoc = common.LoadCommonCodeById(trinhDoID);
            if (!ngayHieuLuc.Year.Equals(1))
            {
                hd.NgayHieuLuc = ngayHieuLuc;
            }
            return hd;
        }
        private void Copy(NSTrinhDoTinHoc src, NSTrinhDoTinHoc des)
        {
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.LoaiTrinhDoTinHocID = src.LoaiTrinhDoTinHocID;
            des.TenTrinhDoTinHoc = src.TenTrinhDoTinHoc;

            if (src.NgayHieuLuc.HasValue)
            {
                des.NgayHieuLuc = src.NgayHieuLuc;
            }
        }
    }
}